import { personInfo ,relogin,getTodayTotal} from "../../apis/gyt"
Page({
  data: { 

    isload:false,
    isRelogin:false,
    person:{name:""},
    stats:{certTotal:0,borTotal:0,returnTotal:0}
 },
 async onShow(){
    if(!this.data.isload){
      await this.onLoadfunc();
    }else{
      var res=await getTodayTotal();
      this.setData({stats:res});
    }
  },
  async onLoadfunc() {
    var token=wx.getStorageSync('token');
    if(!token){
      return wx.navigateTo({ url: '/pages/login/login' });
    }
    var res=await personInfo().catch(err=>{
      wx.navigateTo({ url: '/pages/error/error' });
      return Promise.reject(err);
    });
    
    if(res.code>500){
      return wx.navigateTo({ url: '/pages/error/error?code'+ res.code});;
    }
    if(res.code==200){
      wx.setNavigationBarTitle({
        title: res.data.libName,
      })
      this.setData({person:res.data});
      this.setData({isload:true});
      wx.setStorageSync('info', res.data);
      var res=await getTodayTotal();
      this.setData({stats:res});
      
    }
    else{

      var res=await relogin();
      if(res.code!=200){
        wx.navigateTo({ url: '/pages/login/login' });
      }else{
        if(!this.data.isRelogin){
          this.setData({isRelogin:true});
          this.onLoadfunc();
        }
      }
    }
    
  },

  navigateToBorrow() { wx.navigateTo({ url: '/pages/auth/auth?type=loan' }) },
  navigateToReturn() { wx.navigateTo({ url: '/pages/return/return' }) },
  navigateToCreateCard() { wx.navigateTo({ url: '/pages/log/log' }) },
  navigateToMake(){ wx.navigateTo({ url: '/pages/make/make' }) },
  navigateToCard(){ wx.navigateTo({ url: '/pages/card/card' }) },
  navigateToLogin() { wx.navigateTo({ url: '/pages/login/login' }) },
  navigateToNotice() { wx.navigateTo({ url: '/pages/notice/notice' }) },
  navigateToRenew(){wx.navigateTo({ url: '/pages/auth/auth?type=renew' })},
})